//
//  ExplocyViewController.m
//  leo
//
//  Created by 曹晓雨 on 16/4/8.
//  Copyright © 2016年 mdx. All rights reserved.
//

#import "ExplocyViewController.h"
#import "GrowManager.h"
#import "GrowManager.h"
#import "DataManager.h"
#import "User.h"
#import "GrowDataManager.h"
#import "Watch.h"
#import "Growth.h"
#import "Tooler.h"
#import "MBFontAdapter.h"

@interface ExplocyViewController ()
@property (weak, nonatomic) IBOutlet UILabel *growNum;
@property (weak, nonatomic) IBOutlet UILabel *dayBeforeYestoday;
@property (weak, nonatomic) IBOutlet UILabel *yestoday;
@property (weak, nonatomic) IBOutlet UILabel *today;
@property (weak, nonatomic) IBOutlet UILabel *continuousDays;
@property (weak, nonatomic) IBOutlet UILabel *accompanyCount;
@property (weak, nonatomic) IBOutlet UILabel *chatCount;
@property (weak, nonatomic) IBOutlet UILabel *telCount;
@property (weak, nonatomic) IBOutlet UILabel *levelLabel;
@property (weak, nonatomic) IBOutlet UIView *statusView;
@property (weak, nonatomic) IBOutlet UIView *useDaysView;
@property (nonatomic, strong)GrowDataManager *growDataManager;
@property (nonatomic, strong)GrowManager *growManager;

@property (nonatomic, strong)DataManager *dataManager;
@property (nonatomic, strong)User *user;
@property (nonatomic,strong) Watch *selectWatch;
@property (nonatomic, strong)Growth *growthData;
@end

static BOOL isChange;
@implementation ExplocyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
        self.extendedLayoutIncludesOpaqueBars=YES;
    self.title=@"成长指数";
    isChange = NO;
    [self initDataProxy];
 

}

- (void)viewWillAppear:(BOOL)animated
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    if (queue == NULL) {
        NSLog(@"%@,ExplocyViewController,queue is null",explocy_tag);
        return;

    }
     dispatch_async(queue, ^{
    [self initData];
     });
   
    [self initUiData];
}
-(void)initDataProxy
{
    _dataManager=[[DataManager alloc] init];
    if(_dataManager==NULL)
        return;
    _user=[User sharedSingleton];
    if(_user==NULL)
        return;
    _growDataManager=[[GrowDataManager alloc] init];
    if(_growDataManager==NULL)
        return;
    _growManager=[[GrowManager alloc] init];
    if(_growManager==NULL)
        return;
}

- (void)initData
{

        dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        if(queue==NULL)
        {
            NSLog(@"%@,ExplocyViewController,queue is null",explocy_tag);
            return;
        }
        dispatch_async(queue, ^{
            if(_growManager==NULL)
                return;
            _selectWatch=[_dataManager getActiveWatch:_user.uid];
            if(_selectWatch==NULL)
                return;
            [_growManager getGrowthData:_selectWatch.deviceUid completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
                if(result==SUCESS)
                {
                    [self initUiData];
        
                }
                else
                {
                    //[Tooler showHttpMsg:result description:description msg:@"获取成长值失败"];
                }
            }];
        
            
        });
                      
 }

-(void)initUiData
{
    if(_growDataManager==NULL)
        return;
    _selectWatch=[_dataManager getActiveWatch:_user.uid];
    if(_selectWatch==NULL)
        return;
    _growthData=[_growDataManager getLatestGrowth:_selectWatch.deviceUid];
    if(_growthData==NULL)
        return;
    
    NSString *contentStr = [NSString stringWithFormat:@"%@%d%@",@"今日成长指数 ",_growthData.emp,@"分"];
  
    NSInteger length = [[NSString stringWithFormat:@"%d",_growthData.emp] length];
    NSMutableAttributedString *changeStr =   [Tooler setFont:[UIFont systemFontOfSize:36] range:NSMakeRange(7, length) text:contentStr];
    _growNum.attributedText = changeStr;
    
    
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    NSInteger interval = [zone secondsFromGMTForDate:date];
    NSDate*  today = [date dateByAddingTimeInterval:interval];
    NSTimeInterval a_day = 24*60*60;
    NSDate *yestoday  = [NSDate dateWithTimeInterval:(-a_day) sinceDate:today];
    NSDate *dayBeforeYestoday = [NSDate dateWithTimeInterval:(-a_day) sinceDate:yestoday];
    
    _today.text =  [Tooler getMonthAndDay:today];
    _yestoday.text = [Tooler getMonthAndDay:yestoday];
    _dayBeforeYestoday.text = [Tooler getMonthAndDay:dayBeforeYestoday];
    _levelLabel.text = [NSString stringWithFormat:@"LV.%d",[Tooler getLevelNum:_growthData.semp]];
    
    NSString *continousDays = [NSString stringWithFormat:@"已连续使用%d天", _growthData.continousDays];
    NSMutableAttributedString *changeDay = [Tooler setFont:[UIFont fontWithName:@"Helvetica-Bold"  size:18] range:NSMakeRange(5, 1) text:continousDays];
    _continuousDays.attributedText = changeDay;
    _accompanyCount.text = [NSString stringWithFormat:@"+ %d次",  _growthData.blueNum];
    _chatCount.text = [NSString stringWithFormat:@"+ %d次", _growthData.chatNum];
    _telCount.text = [NSString stringWithFormat:@"+ %d次",_growthData.callNum];
    

    _statusView.layer.borderColor =  [UIColor colorWithRed:0.79 green:0.88 blue:0.92 alpha:1].CGColor;

    _statusView.layer.borderWidth = 1.0;
    
    _useDaysView.layer.borderColor =  [UIColor colorWithRed:0.79 green:0.88 blue:0.92 alpha:1].CGColor;
    _useDaysView.layer.borderWidth = 1.0;
    
    [self changeFont];
    isChange = YES;
}

- (void)changeFont
{
    if (isChange == NO) {
        _growNum.font = [MBFontAdapter adjustFont:_growNum.font];

    }
  }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
 
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
